﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using ChinaSoft.BLL;
using ChinaSoft.Model;
using System.Collections.Generic;

namespace Maticsoft.Web.Accounts
{
	/// <summary>
	/// Add 的摘要说明。
	/// </summary>
	public partial class Add : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlInputButton btnCancel;
		public string adminname="管理部门";
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!IsPostBack)
			{
                BindRoleData();				
			}
		}

        private void BindRoleData()
        {
            Model_tb_MidSoft_Roles roles = new Model_tb_MidSoft_Roles();
            BLL_tb_MidSoft_Roles rolesMgr = new BLL_tb_MidSoft_Roles();
            ChinaSoft.Common.BindControl.BindDropDownList(ddlRole,rolesMgr.GetAllList(),"RoleName", "RolesId");
        }


		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

        protected void btnSave_Click(object sender, System.EventArgs e)
		{
            string strErr = "";
            if (this.txtUserAccount.Value.Trim().Length == 0)
            {
                strErr += "用户帐号不能为空！\\n";
            }
            if (this.txtUserPassword.Text.Trim().Length == 0)
            {
                strErr += "用户密码不能为空！\\n";
            }
            if (this.txtUserName.Text.Trim().Length == 0)
            {
                strErr += "用户名不能为空！\\n";
            }
            if (this.txtCompany.Text.Trim().Length == 0)
            {
                strErr += "单位名不能为空！\\n";
            }
            if (this.txtDepartMent.Text.Trim().Length == 0)
            {
                strErr += "部门不能为空！\\n";
            }
            if (!Maticsoft.Common.PageValidate.IsDateTime(txtBirthDay.Value))
            {
                strErr += "出生日期格式错误！\\n";
            }
            if (this.txtTel.Text.Trim().Length == 0)
            {
                strErr += "联系电话不能为空！\\n";
            }
            if (this.txtDuty.Text.Trim().Length == 0)
            {
                strErr += "Duty不能为空！\\n";
            }
            if (strErr != "")
            {
                Maticsoft.Common.MessageBox.Show(this, strErr);
                return;
            }
            string UserAccount = this.txtUserAccount.Value;
            string UserPassword = Maticsoft.Common.DEncrypt.DEncrypt.MD5(this.txtUserPassword.Text.Trim(),16);
            string UserName = this.txtUserName.Text;

            string Company = this.txtCompany.Text;
            string DepartMent = this.txtDepartMent.Text;
            string Sex = this.rbtnMen.Checked?"男":"女";
            DateTime BirthDay = DateTime.Parse(this.txtBirthDay.Value);
            string Tel = this.txtTel.Text;
            string Duty = this.txtDuty.Text;
            DateTime CreateTime = DateTime.Now;
            int CreateUserId=0;
            if (Session["currentUserID"] != null && Session["currentUserID"].ToString() != "")
                CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);

            int State =chboxKaiTong.Checked?0:1;
            #region 图片上传
            string UserImgFolder = "";
            string UserImgPath = "default.jpg";
            string result = ChinaSoft.Common.UpFile.UploadFile(FileUpload1, Server.MapPath("/Uploads"), "", 5);
            if (result.Contains("错误"))
            {
                Maticsoft.Common.MessageBox.Show(this, result);
                return;
            }
            else
            {
                UserImgFolder = ChinaSoft.Common.UpFile.GetDateFolderPath("/Uploads/Header");
                UserImgPath = result;
            }
            #endregion
            ChinaSoft.Model.Model_tb_MidSoft_Users model = new ChinaSoft.Model.Model_tb_MidSoft_Users();
            model.UserAccount = UserAccount;
            model.UserPassword = UserPassword;
            model.UserName = UserName;
            model.UserImgFolder = UserImgFolder;
            model.UserImgPath = UserImgPath;
            model.Company = Company;
            model.DepartMent = DepartMent;
            model.Sex = Sex;
            model.BirthDay = BirthDay;
            model.Tel = Tel;
            model.Duty = Duty;
            model.CreateTime = CreateTime;
            model.CreateUserId = CreateUserId;
            model.State = State;

            ChinaSoft.BLL.BLL_tb_MidSoft_Users bll = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            bll.Add(model);
            #region 添加用户成功日志
            BLL_tb_MidSoft_log logMgr = new BLL_tb_MidSoft_log();
            Model_tb_MidSoft_log log = new Model_tb_MidSoft_log();
            log.CreateTime = DateTime.Now;
            log.description = "添加用户成功！";
            log.userAccount = Session["UserAccount"].ToString();
            log.userName = Session["UserName"].ToString();
            logMgr.Add(log);
            #endregion
            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "add.aspx");
		
		}

        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("userlist.aspx");
        }
	}
}
